package com.weixin.paleituo.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.UpdateInfoBean;
import com.weixin.paleituo.eventbus.UpdataInfoEvent;
import com.weixin.paleituo.help.TextWatcherImp;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.http.RxHelper;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LoadingDialogUtils;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class NikeNameActivity extends BasePersonActivity {


    @BindView(R.id.iv_del_activity_nike_name)
    ImageView mIvDelActivityNikeName;
    private View mContentView;
    private EditText mETNike;
    private String mNickname;
    private Dialog mLoadingDialog;

    @Override
    protected View getContentView(Context context) {
        mContentView = UIUtils.inflate(this, R.layout.activity_nike_name);
        mETNike = mContentView.findViewById(R.id.et_nike_activity_nike);
        return mContentView;
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, NikeNameActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mIvDelActivityNikeName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mETNike.setText("");

            }
        });


    }



    @Override
    protected void save(View v) {
        super.save(v);
        String trim = mETNike.getText().toString().trim();

        if (StringUtils.isEmpty(trim)) {
            ToastUtils.makeText("请输入内容");
            return;
        }

        if (!UIUtils.isNick(trim)){
            ToastUtils.makeText("昵称只能输入汉字、字母或数字");
            return;
        }
        if (!StringUtils.isEmpty(mNickname) && mETNike.equals(mNickname)) {
            finish();
        } else {
            uploadHeadPic(trim, "", "", "");
        }

        //上传

    }

    /**
     * 上传头像url
     *
     * @param
     */
    private void uploadHeadPic(String nick, String sex, String signature, String image) {
        Observable<StatusCode<UpdateInfoBean>> statusCodeObservable = ApiUtils.getApi().updatePersonInfo(BaseApplication.getToken(), BaseApplication.sUserInfoBean.userId, nick, sex, signature, image)
                .compose(RxHelper.getObservaleTransformer()).doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (mLoadingDialog == null) {
                            mLoadingDialog = LoadingDialogUtils.createLoadingDialog(NikeNameActivity.this, "");
                        }
                        LoadingDialogUtils.show(mLoadingDialog);


                    }
                }).subscribeOn(AndroidSchedulers.mainThread());
        HttpUtil.getInstance().toSubscribe(statusCodeObservable, new ProgressSubscriber<UpdateInfoBean>(this) {
            @Override
            protected void _onNext(StatusCode<UpdateInfoBean> t) {
                UpdateInfoBean o = t.getData();

                if (o != null) {
                    ToastUtils.makeText("保存成功");
                    LogUtils.i(TAG, "更新" + o.toString());
                    String imgurl = o.getImgurl();
                    String nickname = o.getNickname();
                    String sex = o.getSex();
                    String signature = o.getSignature();
                    EventBus.getDefault().post(new UpdataInfoEvent(imgurl, signature, sex, nickname));
                    LoadingDialogUtils.closeDialog(mLoadingDialog);
                    finish();
                } else {
                    ToastUtils.makeText(t.getDetailMsg() + "");
                }

            }

            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);
                LogUtils.e(TAG, "错误" + message);
                LoadingDialogUtils.closeDialog(mLoadingDialog);

            }
        }, "", lifecycleSubject, false, true);
    }
    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText("修改昵称");
        mTvSmalTitle.setText("保存");
        mTvSmalTitle.setVisibility(View.VISIBLE);
        mNickname = BaseApplication.sUserInfoBean.nickname;
        if (!StringUtils.isEmpty(mNickname)) {
            mETNike.setText(mNickname);
            mIvDelActivityNikeName.setVisibility(View.VISIBLE);
        }else {
            mIvDelActivityNikeName.setVisibility(View.INVISIBLE);
        }
        mETNike.requestFocus();
        mETNike.setSelection(mETNike.getText().toString().length());

        mETNike.addTextChangedListener(new TextWatcherImp() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                super.beforeTextChanged(s, start, count, after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String contnet = s.toString();

                if (StringUtils.isEmpty(contnet)){
                    mIvDelActivityNikeName.setVisibility(View.INVISIBLE);

                }else {
                    mIvDelActivityNikeName.setVisibility(View.VISIBLE);

                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
            }
        });






    }

}
